<?php

namespace App\Sidong\Controllers;

use App\Admin\Extensions\CancelMember;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class MemberController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员管理';

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '会员列表'])
            ->body($this->grid());
    }

    public function edit($id, Content $content)
    {
        return $content
            ->title('编辑用户')
            ->breadcrumb(['text' => '会员列表', 'url' => 'members'], ['text' => '编辑会员'])
            ->body($this->form()->edit($id));
    }

    public function store()
    {
        $data = \request()->all();
        if ($message = $this->form()->validationMessages($data)) {
            return back()->withInput()->withErrors($message);
        }
        $user = User::where(['phone' => $data['phone']])->first();
        if ($user->is_member) {
            admin_toastr('该用户已经是会员了', 'error');
            return redirect()->back()->withInput();
        }
        $user->is_member = true;
        $user->nickname = $data['nickname'];
        $user->member_expired_time = $data['member_expired_time'];
        $user->member_remain_times = $data['member_remain_times'];
        $user->save();
        admin_toastr('操作成功');
        return redirect(admin_url('members'));
    }

    public function cancel($id)
    {
        $user = User::find($id);
        $user->is_member = false;
        $user->member_expired_time = null;
        $user->member_remain_times = 0;
        $user->save();

        return response()->json([
            'status' => true,
            'message' => '操作成功'
        ]);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $admin = \Admin::user();
        $grid->model()->where('tenant_id', $admin->tenant_id)->where('is_member', true);
        $grid->disableBatchActions();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
            $actions->disableDelete();
            $actions->append(new CancelMember($actions->getKey()));
        });
        $grid->quickSearch('phone')->placeholder('搜索手机号码/昵称/OPENID');

        $grid->column('id', 'ID');
        $grid->column('phone', '手机号码');
        $grid->column('nickname', '昵称');
        $grid->column('realname', '真实姓名');
        $grid->column('weapp_openid', 'OPENID')->copyable();
        $grid->column('member_expired_time', '会员时卡过期时间');
        $grid->column('member_remain_times', '会员次卡剩余次数');
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);
        if ($form->isCreating()) {
            $form->text('phone', '手机号码')->help('请输入手机号码，该号码必须已经注册了用户')->rules('required|exists:users,phone', [
                'required' => '手机号码 不能为空',
                'exists' => '该手机号码还未注册用户',
            ]);
            $form->text('nickname', '昵称')->rules('required');
            $form->datetime('member_expired_time', '会员时卡过期时间')->width('200px')->help('设置会员卡过期时间，不填写默认是无限期');
            $form->number('member_remain_times', '会员次卡剩余次数')->min(0)->help('设置会员次卡剩余次数，不填写默认是0');
        } else {
            $form->text('phone', '手机号码')->readonly();
            $form->text('nickname', '昵称')->rules('required');
            $form->text('weapp_openid', 'OPENID')->readonly();
            $form->datetime('member_expired_time', '会员时卡过期时间')->width('200px')->help('修改后，用户的会员卡有效期将会变更');
            $form->number('member_remain_times', '会员次卡剩余次数')->min(0)->help('修改后，用户的会员卡有效次数将会变更');
        }

        return $form;
    }
}
